// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Sliding Floor Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.startPos.x

private alias 0 : SLIDINGFLOOR_WAITFORPLAYER
private alias 1 : SLIDINGFLOOR_SLIDEAWAY
private alias 2 : SLIDINGFLOOR_FINISHEDSLIDING
private alias 3 : SLIDINGFLOOR_RETURNTOSTART

// Player Aliases
private alias object.xpos : player.xpos


// ========================
// Function Declarations
// ========================

reserve function SlidingFloor_DebugDraw
reserve function SlidingFloor_DebugSpawn


private function SlidingFloor_DebugDraw
	DrawSprite(0)
end function


private function SlidingFloor_DebugSpawn
	CreateTempObject(TypeName[Sliding Floor], 0, object.xpos, object.ypos)
	if object.direction == FLIP_NONE
		object[tempObjectPos].xvel = 0x80000
	else
		object[tempObjectPos].xvel = -0x80000
	end if
	object[tempObjectPos].startPos.x = object.xpos
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case SLIDINGFLOOR_WAITFORPLAYER
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -64, -12, 64, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				object.priority = PRIORITY_ACTIVE
				object.state++
			end if
		next
		break

	case SLIDINGFLOOR_SLIDEAWAY
		object.timer++
		if object.timer == 16
			object.timer = 0
			object.state++
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -64, -12, 64, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				player[currentPlayer].xpos += object.xvel
			end if
		next
		object.xpos += object.xvel
		break

	case SLIDINGFLOOR_FINISHEDSLIDING
		object.timer++
		if object.timer == 360
			object.timer = 0
			object.state++
		end if

		if object.timer < 3
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_PLATFORM, object.entityPos, -72, -12, 72, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			next
		else
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_PLATFORM, object.entityPos, -64, -12, 64, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			next
		end if
		break

	case SLIDINGFLOOR_RETURNTOSTART
		object.timer++
		if object.timer == 16
			object.timer = 0
			object.state = SLIDINGFLOOR_WAITFORPLAYER
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -64, -12, 64, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				player[currentPlayer].xpos -= object.xvel
			end if
		next
		object.xpos -= object.xvel
		break
		
	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		object.xpos = object.startPos.x
		
		if object.outOfBounds == true
			object.timer = 0
			object.priority = PRIORITY_BOUNDS
			object.state = SLIDINGFLOOR_WAITFORPLAYER
		else
			object.xpos = temp0
		end if
	end if
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-64, -12, 128, 24, 383, 115)
	
	foreach (TypeName[Sliding Floor], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].propertyValue &= 1
		if object[arrayPos0].propertyValue == 0
			object[arrayPos0].xvel = 0x80000
		else
			object[arrayPos0].xvel = -0x80000
		end if
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
	next

	SetTableValue(TypeName[Sliding Floor], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(SlidingFloor_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(SlidingFloor_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		case 0 // direction
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		case 0 // direction
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-64, -12, 128, 24, 383, 115)

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Right", 0)
	AddEnumVariable("Left", 1)
end event
